

import haxe.ds.GenericStack;
import h2d.Object;

typedef ObjMap = Map<String, GenericStack<Object>>;

class ObjPool {
    static var MAX_COUNT = 10;
    static var instance : ObjPool;
    var map : ObjMap;

    public function new() {
        map = new ObjMap();
    }

    public static function GetInstance()
    {
        if(instance == null){
            instance = new ObjPool();
        }
        return instance;
    }

    public function Get<T:Object>(t:Class<T>) : T {
        var key = Std.string(t);
        var stack = map[key];
        if(stack == null){
            stack = new GenericStack<Object>();
            map[key] = stack;
        }
        var obj : T;
        obj = cast stack.pop();
        if(obj == null){
            obj = std.Type.createInstance(t, []);
        }
        return obj;
    }

    public function Set(obj : Object) {
        var key = Std.string(Type.getClass(obj));
        var stack = map[key];
        stack.add(obj);
        obj = null;
    }
    
}

